﻿using System;
using System.Collections.Generic;
using System.Data.Odbc;
using System.Linq;
using System.Text;
using RiskMan.Config;
using RiskMan.DataClass.Models.Gilevskiy;
using System.Text.RegularExpressions;
using Ciloci.Flee;
using RiskMan.DataClass.Models.NotBasModels;

namespace RiskMan.Views.Gilevskiy
{

    /// <summary>
    /// Основная логика робота
    /// </summary>
    class LogikRobot
    {

        private DataClass.Data data;
        private MainWindow mainWindow;
        private RobotGilevskiyItem robot;
        double TTP_Last;
        string Classcode;
        int accuraccy;

        public LogikRobot(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;
            data = mainWindow._dataProvider;
            this.robot = robot;
        }

        public void Start(RobotGilevskiyItem robotItem)
        {
            //цена последней сделки
            TTP_Last = mainWindow._dataProvider.GetLastPrice(robotItem.Instrument);
            Classcode = mainWindow._dataProvider.GetClasscode(robotItem.Instrument);
            accuraccy = mainWindow._dataProvider.GetAccuraccy(robotItem.Instrument);
            robotItem.LastPrice = TTP_Last;

            try
            {
                ExpressionContext context = new ExpressionContext();
                context.Imports.AddType(typeof(Math));
                LogikConditionToDitgit logik = new LogikConditionToDitgit();
                robotItem.ResultConditionDitgit = logik.ConvertConditionFromStringToDitgit(robotItem);

                IGenericExpression<bool> eGeneric = context.CompileGeneric<bool>(robotItem.ResultConditionDitgit);

                robotItem.ResultCondition = eGeneric.Evaluate();
            }
            catch
            {
                return;
            }

            if (robotItem.ResultCondition && robotItem.TradeList.Count == 0)
            {
                Cfg.SetLog(robotItem.Comment + ". Вход в позицию " + robotItem.ResultConditionDitgit, mainWindow.ListBox_Logs);
                robotItem.TradeList.Add(new TradesOpen());
                robotItem.TradeList[0].OpenStatusOrder = Cfg.Performed;
            }

            if (robotItem.TradeList.Count > 0)
                if (robotItem.TradeList[0].OpenStatusOrder == Cfg.Performed && !robotItem.ResultCondition)
                {
                    robotItem.TradeList[0].OpenStatusOrder = null;
                    robotItem.TradeList.Clear();
                }

        }



    }



}
